﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public class DockSplitControl : System.Windows.Forms.SplitContainer
    {
        public DockSplitControl(DockPanel dockPanel)
        {
            //base.Panel1Collapsed = true;
            this.m_DockPanel = dockPanel;
            this.ParentChanged += DockSplitControlParentChanged;
            this.Panel1.ControlAdded += DockSplitControlPanelControlAdded;
            this.Panel2.ControlAdded += DockSplitControlPanelControlAdded;
        }

        private void DockSplitControlParentChanged(object sender, EventArgs e)
        {
            var parent = (this.Disposing || this.IsDisposed) ? null : this.Parent;
            if (parent == null || parent.Disposing || parent.IsDisposed) return;
            if (parent is DockPanel) return;
            if (parent is SplitterPanel) return;

            throw new Exception(string.Format("DockSplitControl 只能是 DockPanel 或者 SplitterPanel 的子控件, 而不能是 类型为 '{0}' 的子控件!", parent.GetType().FullName));
        }
        private void DockSplitControlPanelControlAdded(object sender, ControlEventArgs e)
        {
            if (e.Control is DockTabGroup) return;
            if (e.Control is DockSplitControl) return;

            throw new Exception(string.Format("DockSplitControl.Panel 的子控件, 只能是 DockTabGroup 或 DockSplitControl 类型, 而不能是 类型为 '{0}' 的子控件!", e.Control.GetType().FullName));
        }


        private DockPanel m_DockPanel;
        public DockPanel DockPanel
        {
            get { return m_DockPanel; }
        }
        public SplitterPanel ParentSplitPanel
        {
            get
            {
                var splitPanel = this.Parent as SplitterPanel;
                return splitPanel;
            }
        }
        public DockSplitControl ParentSplitControl
        {
            get
            {
                var parent = this.ParentSplitPanel;
                var splitCtrl = (parent != null) ? parent.Parent as DockSplitControl : null;
                return splitCtrl;
            }
        }


        public void DoLayout()
        {
            var ctrls1 = this.Panel1.Controls;
            var ctrls2 = this.Panel2.Controls;
            var ctrl1 = ctrls1.Count >= 1 ? ctrls1[0] : null;
            var ctrl2 = ctrls2.Count >= 1 ? ctrls2[0] : null;

            #region  处理 已关闭的 部分

            if (ctrl1 == null && ctrl2 == null)
            {
                #region  释放当前 SplitControl

                var splitCtrl = this.ParentSplitControl;
                this.Dispose();
                splitCtrl.DoLayout();
                return;

                #endregion
            }
            if ((ctrl1 == null && ctrl2 != null) || (ctrl2 == null && ctrl1!=null))
            {
                #region  执行合并, 释放当前 SplitControl

                var parent = this.Parent;
                var ctrl = ctrl1 ?? ctrl2;
                ctrl.Parent = null;
                this.Dispose();
                ctrl.Parent = parent;
                return;

                #endregion
            }

            #endregion


            #region  处理 已折叠的 部分

            var riveted1 = SplitPanelRiveted(this.Panel1);
            var riveted2 = SplitPanelRiveted(this.Panel2);
            if (!riveted1 && !riveted2)
            {
                var splitCtrl = this.ParentSplitControl;
                if (splitCtrl != null) splitCtrl.DoLayout();
                return;
            }
            if ((riveted1 && !riveted2) || (!riveted1 && riveted2))
            {
                this.Panel1Collapsed = !riveted1;
                this.Panel2Collapsed = !riveted2;
                var splitCtrl = this.ParentSplitControl;
                if (splitCtrl != null) splitCtrl.DoLayout();
                return;
            }
            if (riveted1 && riveted2) //时钟为 true
            {
                this.Panel1Collapsed = false;
                this.Panel2Collapsed = false;
                var splitCtrl = this.ParentSplitControl;
                if (splitCtrl != null) splitCtrl.DoLayout();
                return;
            }

            #endregion
        }




        public virtual new void Dispose()
        {
            this.ParentChanged -= DockSplitControlParentChanged;
            this.Panel1.ControlAdded -= DockSplitControlPanelControlAdded;
            this.Panel2.ControlAdded -= DockSplitControlPanelControlAdded;
            this.Parent = null;
            this.m_DockPanel = null;
            base.Dispose();
        }


        public static bool SplitPanelRiveted(SplitterPanel panel)
        {
            if (panel == null) return false;
            var ctrls = panel.Controls;
            var ctrl = ctrls.Count >= 1 ? ctrls[0] : null;
            if (ctrl is DockTabGroup) return ((DockTabGroup)ctrl).Riveted;
            if (ctrl is DockSplitControl) return SplitControlRiveted((DockSplitControl)ctrl);
            return false;
        }
        public static bool SplitControlRiveted(DockSplitControl control)
        {
            if (control == null) return false;
            var riveted1 = SplitPanelRiveted(control.Panel1);
            if (riveted1) return true;
            var riveted2 = SplitPanelRiveted(control.Panel2);
            if (riveted2) return true;
            return false;
        }
    }
}
